package util;

import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;

import vo.Cal;
import vo.Task;
import vo.TaskList;

public class CalSerializer {


	//TODO
	public static Cal load(String path) throws IOException {

		return unMarshallCal(path);

	}

	

	//TODO
	public static void save(Cal sCal, String path) {

		try {
			
			File file = new File(path);
			
			BufferedWriter output = new BufferedWriter(new FileWriter(file));
			output.write(marshallCal(sCal));
			output.close();
			

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}


	// unMarshall file from path
	public static Cal unMarshallCal(String path) throws IOException {

		try {

			FileInputStream in = new FileInputStream(path);

			// create an instance context class, to serialize/deserialize.
			JAXBContext jaxbContext = JAXBContext.newInstance(Cal.class);

			return (Cal) jaxbContext.createUnmarshaller().unmarshal(in);

		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}


		return null;
	}

	//TODO
	public static String marshallCal(Cal c) {

		StringWriter writer = new StringWriter();

		try {
			JAXBContext jaxbContext = JAXBContext.newInstance(Cal.class);
			jaxbContext.createMarshaller().marshal(c, writer);



		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	

		return writer.toString();

	}
	
	public static String marshallTaskList(TaskList tList) {

		StringWriter writer = new StringWriter();

		try {
			JAXBContext jaxbContext = JAXBContext.newInstance(TaskList.class);
			jaxbContext.createMarshaller().marshal(tList, writer);


		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	

		return writer.toString();

	}
	
	public static TaskList unMarshallTaskList(String tList) throws IOException {

		try {
			
			ByteArrayInputStream in = new ByteArrayInputStream(tList.getBytes("UTF8"));
	

			JAXBContext jaxbContext = JAXBContext.newInstance(TaskList.class);

			return (TaskList) jaxbContext.createUnmarshaller().unmarshal(in);
			

		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}


		return null;
	}

	public static String marshallTask(Task tList) {

		StringWriter writer = new StringWriter();

		try {
			JAXBContext jaxbContext = JAXBContext.newInstance(Task.class);
			jaxbContext.createMarshaller().marshal(tList, writer);


		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	

		return writer.toString();

	}
	
	public static Task unMarshallTask(String t) throws IOException {

		try {
			
			ByteArrayInputStream in = new ByteArrayInputStream(t.getBytes("UTF8"));
	

			JAXBContext jaxbContext = JAXBContext.newInstance(Task.class);

			return (Task) jaxbContext.createUnmarshaller().unmarshal(in);
			

		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}


		return null;
	}
	

}
